var token;
var u_id;
var num;
var grade;
var i_id;

var bank_list_num;
var now_bankId = 0;

//接收多值的js函数
function manyValues() {
    var url = window.location.search;
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        key = [token, u_id, num, i_id];
        var value = new Array(strs.length);
        for (i = 0; i < strs.length; i++) {
            value[i] = unescape(strs[i].split("=")[1]);
            key[i] = value[i];
        }
    }
}

manyValues();
token = key[0];
u_id = key[1];
num = key[2];
i_id = key[3];
getGrade();

//查询管理员的班级
function getGrade() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        type: "GET",
        url: "/api/user/getGradeByNum?num=" + num,
        async: false,
        success: function (result) {
            grade = result.data;
        }
    });
}

listBank();

//查询题库
function listBank() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        contentType: 'application/json; charset=utf-8',
        type: "POST",
        url: "/api/examination/listBankByInstitutionGrade",
        data: JSON.stringify({
            "institutionId": i_id,
            "grade": grade,
        }),
        async: false,
        success: function (result) {
            var txt = "";
            var list = result.data;
            bank_list_num = list.length;
            for (var i = 0; i < list.length; i++) {
                var txt2 = '<button class="btn-bank" value=' + list[i].bankId + '>' + list[i].bankName + '</button>'
                txt += txt2;
            }
            $("#div-bank").html(txt)
        }
    });
}

for (var i = 0; i < bank_list_num; i++) {
    let index = i;
    $($(".btn-bank")[i]).click(function () {
        now_bankId = $($(".btn-bank")[index]).val();
        $.ajax({
            headers: {
                'Authorization': token,
            },
            contentType: 'application/json; charset=utf-8',
            type: "POST",
            url: "/api/examination/listQuestionByBankId?bankId=" + now_bankId,
            async: false,
            success: function (result) {
                var txt = "";
                var list = result.data;
                bank_list_num = list.length;
                for (var i = 0; i < list.length; i++) {
                    txt = txt + '<p>题目：' + list[i].question + '</p>\n' +
                        '    <p>参考答案：' + list[i].answer + '</p>'
                }
                $("#div-question").html(txt)
            }
        });
    })
}

//添加题目
$("#btn-addQuestion").click(function () {
    if (now_bankId == 0) {
        alert("您还没有选择添加的题库！");
        return;
    }
    $.ajax({
        headers: {
            'Authorization': token,
        },
        contentType: 'application/json; charset=utf-8',
        type: "POST",
        url: "/api/examination/addQuestionByBankId",
        data: JSON.stringify({
            "bankId": now_bankId,
            "question": $("#new-question").val(),
            "answer": $("#new-question-answer").val()
        }),
        async: false,
        success: function (result) {
            alert(result.message)
        }
    });
})

//添加题库
$("#btn-addBank").click(function () {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        contentType: 'application/json; charset=utf-8',
        type: "POST",
        url: "/api/examination/saveBank",
        data: JSON.stringify({
            "bankName": $("#new-bankName").val(),
            "institutionId": i_id,
            "grade": grade
        }),
        async: false,
        success: function (result) {
            alert(result.message)
        }
    });
})
